package com.talent.web.mqtt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;

/**
 * @Author: hszhou
 * @Date: 2025/1/8 21:02
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Configuration
public class MqttOutboundConfiguration {

    //消息通道

    @Autowired
    private MqttConfigurationProperties mqttConfigurationProperties;


    @Autowired
    private MqttPahoClientFactory mqttClientFactory;



    @Bean
    public MessageChannel outPutChannel(){
        DirectChannel directChannel = new DirectChannel();
        directChannel.subscribe(messageOutHandler());
        return directChannel;
    }

    //配置入栈是配置 设置订阅的主题以及指定消息的相关熟悉


    @Bean
    @ServiceActivator(inputChannel ="outPutChannel")
    public MessageHandler messageOutHandler(){
        MqttPahoMessageHandler mqttPahoMessageHandler = new MqttPahoMessageHandler(mqttConfigurationProperties.getUrl(),
                mqttConfigurationProperties.getPubClientId(), mqttClientFactory);

        mqttPahoMessageHandler.setAsync(true);
        mqttPahoMessageHandler.setDefaultQos(0);

        mqttPahoMessageHandler.setDefaultTopic("default");

        return mqttPahoMessageHandler;
    }
}
